﻿using System.Web.Mvc;
using PortalCurriculo.Domain;
using PortalCurriculo.MvcWebUI.Helpers;
using PortalCurriculo.MvcWebUI.Models;

namespace PortalCurriculo.MvcWebUI.Controllers
{
    public class ProfissionalController : ControllerBase<ProfissionalModel, Profissional>
    {
        [UserAuthorize(UserType = UserAuthorizeAttribute.UserTypes.PessoaFisica)]
        public ActionResult Curriculos()
        {
            var profissional = WebSession<Profissional>(SessionName.ProfissionalLogado);
            if (profissional != null)
                return base.Detalhes(profissional.Id);

            var usuario = WebSession<Usuario>(SessionName.UsuarioLogado);
            return View(new ProfissionalModel{IdUsuario = usuario.Id});
        }

        [UserAuthorize(UserType = UserAuthorizeAttribute.UserTypes.PessoaFisica)]
        public override ActionResult Manter(int? id)
        {
            base.Manter(id);
            return PartialView();
        }

        [UserAuthorize(UserType = UserAuthorizeAttribute.UserTypes.PessoaFisica)]
        public override ActionResult Manter(ProfissionalModel model)
        {
            if (model.IdUsuario == 0)
            {
                var usuario = WebSession<Usuario>(SessionName.UsuarioLogado);
                model.IdUsuario = usuario.Id;
            }

            if (model.Id == 0)
            {
                var profissional = WebSession<Profissional>(SessionName.ProfissionalLogado);
                if (profissional != null)
                    model.Id = profissional.Id;
            }

            base.Manter(model);

            WebSession(SessionName.ProfissionalLogado, Model.CriarDominio());

            return Json(new { success = true, html = GetControlData("~/Views/Profissional/Detalhes.cshtml", Model), idProfissional = Model.Id });
        }
    }
}
